package com.cb.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.eduservice.entity.EduCourse;
import com.cb.eduservice.entity.EduCourseDescription;
import com.cb.eduservice.entity.vo.CourseInfoVo;
import com.cb.eduservice.entity.vo.CoursePublishVo;
import com.cb.eduservice.entity.vo.CourseQuery;
import com.cb.eduservice.entity.vo.front.CourseFrontVo;
import com.cb.eduservice.entity.vo.front.CourseWebVo;
import com.cb.eduservice.mapper.EduCourseMapper;
import com.cb.eduservice.service.EduChapterService;
import com.cb.eduservice.service.EduCourseDescriptionService;
import com.cb.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cb.eduservice.service.EduVideoService;
import com.cb.servicebase.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Cebb
 * @since 2020-06-22
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    private final EduCourseDescriptionService eduCourseDescriptionService;
    private final EduVideoService videoService;
    private final EduChapterService chapterService;

    @Autowired
    public EduCourseServiceImpl(EduCourseDescriptionService eduCourseDescriptionService, EduVideoService videoService, EduChapterService chapterService) {
        this.eduCourseDescriptionService = eduCourseDescriptionService;
        this.videoService = videoService;
        this.chapterService = chapterService;
    }

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        // 1.向课程表添加课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        eduCourse.setIsDeleted(0);
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0) {
            throw new GuliException(20001, "添加课程信息失败");
        }

        String cid = eduCourse.getId();
        // 2.向课程简介表添加课程简介 edu_course_description
        EduCourseDescription eduCourseDesc = new EduCourseDescription();
        eduCourseDesc.setDescription(courseInfoVo.getDescription());
        eduCourseDesc.setId(cid);
        eduCourseDescriptionService.save(eduCourseDesc);

        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfoByCourseId(String courseId) {
        // 根据课程id查询基本信息
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);
        // 根据课程id查询对应描述
        EduCourseDescription desc = eduCourseDescriptionService.getById(courseId);
        // 封装
        courseInfoVo.setDescription(desc.getDescription());

        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(String id, CourseInfoVo courseInfoVo) {
        courseInfoVo.setId(id);

        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int courseFlag = baseMapper.updateById(eduCourse);
        if (courseFlag == 0) {
            throw new GuliException(20001, "修改课程信息失败");
        }

        EduCourseDescription desc = new EduCourseDescription();
        desc.setDescription(courseInfoVo.getDescription());
        desc.setId(courseInfoVo.getId());
        boolean descFlag = eduCourseDescriptionService.update(desc, null);
        if (!descFlag) {
            throw new GuliException(20001, "修改课程描述失败");
        }
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {

        return baseMapper.getPublishCourseInfo(id);
    }

    @Override
    public IPage<EduCourse> getCourseByPage(long current, long limit, CourseQuery courseQuery) {
        Page<EduCourse> page = new Page<>(current, limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
        IPage<EduCourse> courseList = baseMapper.selectPage(page, wrapper);

        return courseList;
    }

    @Override
    public boolean removeCourse(String courseId) {
        //1.删除小节
        videoService.removeByCourseId(courseId);
        //2.删除章节
        chapterService.removeByCourseId(courseId);
        //3.删除描述
        eduCourseDescriptionService.removeById(courseId);
        //4.删除课程
        int flag = baseMapper.deleteById(courseId);

        return flag > 0;
    }

    @Override
    public List<EduCourse> getCourseByTeacherId(String teacherId) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", teacherId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> page, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String subjectParentId = courseFrontVo.getSubjectParentId();
        String subjectId = courseFrontVo.getSubjectId();
        String buyCountSort = courseFrontVo.getBuyCountSort();
        String gmtCreateSort = courseFrontVo.getGmtCreateSort();
        String priceSort = courseFrontVo.getPriceSort();
        if (!StringUtils.isEmpty(subjectParentId)) {
            wrapper.eq("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            wrapper.eq("subject_id", subjectId);
        }
        if (!StringUtils.isEmpty(buyCountSort)) {
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(gmtCreateSort)) {
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(priceSort)) {
            wrapper.orderByDesc("price");
        }
        baseMapper.selectPage(page, wrapper);
        Map<String, Object> map = new HashMap<>(10);
        map.put("items", page.getRecords());
        map.put("current", page.getCurrent());
        map.put("pages", page.getPages());
        map.put("size", page.getSize());
        map.put("total", page.getTotal());
        map.put("hasNext", page.hasNext());
        map.put("hasPrevious", page.hasPrevious());
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {

        return baseMapper.getBaseCourseInfo(courseId);
    }

}